//matrix.cpp
#include <stdio.h>
#include "Matrix.h"
#include <iostream>
#include <string.h>

using namespace std;

unsigned int matrices = 0;

Matrix::Matrix(const int n, const int ld, bool zero) : m_columnCount(n), m_rowCount(n), m_ld(ld)
{
	Fill(zero);
}

Matrix::Matrix(const int cols, const int rows, const int ld, bool zero) : m_columnCount(cols), m_rowCount(rows), m_ld(ld)
{
	Fill(zero);
}

void Matrix::Fill(bool zero)
{
	int matrixSize = m_ld*m_rowCount;
	m_dpAdr = new double[matrixSize];
	matrices++;
	if(zero)
	{
		memset (m_dpAdr,'\0',m_ld*m_rowCount*sizeof(double));
	}
	else
	{
		for(int i = 0; i < matrixSize; ++i)
		{
			m_dpAdr[i] = i+1;
		}
	}	
}


//const double *Matrix::GetMatrix() const
double const *Matrix::GetMatrix() const
{
	return m_dpAdr;
}

double *Matrix::GetMatrix() 
{
	return m_dpAdr;
}

int Matrix::GetColumnCount() const
{
	return m_columnCount;
}

int Matrix::GetRowCount() const
{
	return m_rowCount;
}

int Matrix::GetLD() const
{
	return m_ld;
}

std::ostream& operator<< (std::ostream &out, const Matrix &self)
{
	string str;
	char cBuff[54];
	for(int i=0 ; i<self.m_columnCount ; ++i)
	{
		for(int j=0 ; j<self.m_rowCount ; ++j)
		{
			sprintf(cBuff, "%*.*f ", 2, 1, self.m_dpAdr[j*self.m_ld+i]);
			//sprintf(cBuff, "%-6d", self.m_dpAdr[i*self.m_ld+j]);
			str += cBuff;
		}
		str += "\n";
	}
	out << str;
	return out;
}

Matrix::~Matrix()
{
matrices--;
	delete [] m_dpAdr;
}
